package com.heima.wemedia.config;

import com.heima.common.constants.message.NewsUpOrDownConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author XHui
 * @Since 2024/2/15 19:59
 * @Version 1.0
 */
@Configuration
public class DeclareUpOrDownRabbitConfig {

    /**
     * 声明 新闻上架或下架 话题交换机
     *
     * @return 话题交流
     */
    @Bean
    public TopicExchange newsUpOrDownTopicExchange() {
        return new TopicExchange(NewsUpOrDownConstants.NEWS_UP_OR_DOWN_EXCHANGE);
    }

    // 声明四个队列
    @Bean
    public Queue newsUpForArticleConfigQueue() {
        return new Queue(NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE, true);
    }

    @Bean
    public Queue newsDownForArticleConfigQueue() {
        return new Queue(NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE, true);
    }

    @Bean
    public Queue newsUpForEsQueue() {
        return new Queue(NewsUpOrDownConstants.NEWS_UP_FOR_ES_QUEUE, true);
    }

    @Bean
    public Queue newsDownForEsQueue() {
        return new Queue(NewsUpOrDownConstants.NEWS_DOWN_FOR_ES_QUEUE, true);
    }

    // 声明绑定关系给，
    @Bean
    public Binding newsUpForArticleConfigBinding() {
        return BindingBuilder
                .bind(newsUpForArticleConfigQueue()) // 将队列 绑定到 话题交换机上
                .to(newsUpOrDownTopicExchange())
                // 绑定路由键，根据这个路由，将消息发送到2个队列中  NEWS_UP_FOR_ES_QUEUE NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE
                .with(NewsUpOrDownConstants.NEWS_UP_ROUTE_KEY)
                ;
    }

    @Bean
    public Binding newsDownForArticleConfigBinding() {
        return BindingBuilder
                .bind(newsDownForArticleConfigQueue()) // 将队列 绑定到 话题交换机上
                .to(newsUpOrDownTopicExchange())
                .with(NewsUpOrDownConstants.NEWS_DOWN_ROUTE_KEY)
                ;
    }

    @Bean
    public Binding newsUpForEsBinding() {
        return BindingBuilder
                .bind(newsUpForEsQueue()) // 将队列 绑定到 话题交换机上
                .to(newsUpOrDownTopicExchange())
                .with(NewsUpOrDownConstants.NEWS_UP_ROUTE_KEY)
                ;
    }

    @Bean
    public Binding newsDownForEsBinding() {
        return BindingBuilder
                .bind(newsDownForEsQueue()) // 将队列 绑定到 话题交换机上
                .to(newsUpOrDownTopicExchange())
                .with(NewsUpOrDownConstants.NEWS_DOWN_ROUTE_KEY)
                ;
    }

}
